<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">

    function test_activate(t) {
        t.plan(2);

        var strategy = new OpenLayers.Strategy.Cluster();
        t.eq(strategy.active, false, "not active after construction");

        var layer = new OpenLayers.Layer.Vector("Vector Layer", {
            strategies: [strategy]
        });
        var map = new OpenLayers.Map('map');
        map.addLayer(layer);
        
        t.eq(strategy.active, true, "active after adding to map");
    }

    function test_clusters(t) {
        t.plan(22);

        function featuresEq(got, exp) {
            var eq = false;
            if(got instanceof Array && exp instanceof Array) {
                if(got.length === exp.length) {
                    for(var i=0; i<got.length; ++i) {
                        if(got[i] !== exp[i]) {
                            console.log(got[i], exp[i]);
                            break;
                        }
                    }
                    eq = (i == got.length);
                }
            }
            return eq;
        }

        var strategy = new OpenLayers.Strategy.Cluster();
        var layer = new OpenLayers.Layer.Vector("Vector Layer", {
            strategies: [strategy],
            isBaseLayer: true
        });
        var map = new OpenLayers.Map('map', {
            resolutions: [4, 2, 1],
            maxExtent: new OpenLayers.Bounds(-40, -40, 40, 40),
            zoomMethod: null
        });
        map.addLayer(layer);
        
        // create features in a line, 1 unit apart
        var features = new Array(80);
        for(var i=0; i<80; ++i) {
            features[i] = new OpenLayers.Feature.Vector(
                new OpenLayers.Geometry.Point(-40 + i, 0)
            );
        }
        
        // add one additional feature, with no geometry - just to confirm it doesn't break things
        features.push(new OpenLayers.Feature.Vector());
        
        layer.addFeatures(features);
        map.setCenter(new OpenLayers.LonLat(0, 0), 0);
        
        // resolution 4
        // threshold: 4 * 20 = 80 units
        // one cluster
        t.eq(layer.features.length, 1, "[4] layer has one cluster");
        t.ok(featuresEq(layer.features[0].cluster, features.slice(0, 80)), "[4] cluster includes all features with geometries");
        
        // resolution 2
        // threshold: 2 * 20 = 40 units
        // two clusters (41 and 39) - first cluster includes all features within 40 units of the first (0-40 or 41 features)
        map.zoomIn();
        t.eq(layer.features.length, 2, "[2] layer has two clusters");
        t.ok(featuresEq(layer.features[0].cluster, features.slice(0, 41)), "[2] first cluster includes first 41 features");
        t.ok(featuresEq(layer.features[1].cluster, features.slice(41, 80)), "[2] second cluster includes last 39 features");
        
        // resolution 1
        // threshold: 1 * 20 = 20 units
        // four clusters (21, 21, 21, and 17)
        map.zoomIn();
        t.eq(layer.features.length, 4, "[1] layer has four clusters");
        t.ok(featuresEq(layer.features[0].cluster, features.slice(0, 21)), "[1] first cluster includes first 21 features");
        t.ok(featuresEq(layer.features[1].cluster, features.slice(21, 42)), "[1] second cluster includes second 21 features");
        t.ok(featuresEq(layer.features[2].cluster, features.slice(42, 63)), "[1] third cluster includes third 21 features");
        t.ok(featuresEq(layer.features[3].cluster, features.slice(63, 80)), "[1] fourth cluster includes last 17 features");
        
        // zoom out and back in to test threshold property (21)
        map.zoomOut();
        strategy.threshold = 21;
        map.zoomIn();
        t.eq(layer.features.length, 20, "[1-threshold 21] layer has 20 clusters");
        t.ok(featuresEq(layer.features[0].cluster, features.slice(0, 21)), "[1-threshold 21] first cluster includes first 21 features");
        t.ok(featuresEq(layer.features[1].cluster, features.slice(21, 42)), "[1-threshold 21] second cluster includes second 21 features");
        t.ok(featuresEq(layer.features[2].cluster, features.slice(42, 63)), "[1-threshold 21] third cluster includes third 21 features");
        t.ok(featuresEq(layer.features.slice(3, 20), features.slice(63, 80)), "[1-threshold 21] last 17 features are not clustered");
        
        // zoom out and back in to test high threshold
        map.zoomOut();
        strategy.threshold = 100; // clusters must contain 100 features or more
        map.zoomIn();
        // the one feature with no geometry is not added to the layer
        t.eq(layer.features.length, features.length-1, "[1-threshold 100] layer has " + (features.length-1) + " clusters");
        t.ok(featuresEq(layer.features, features.slice(0, 80)), "[1-threshold 100] layer has all features with geometry");

        // remove features and zoom
        strategy.threshold = 1;
        map.setCenter(new OpenLayers.LonLat(0, 0), 0);
        t.eq(strategy.features.length, 81,
                    "[remove features] cluster has cache");
        layer.removeAllFeatures();
        t.eq(layer.features.length, 0,
                    "[remove features] layer has no features after remove");
        t.ok(!strategy.features,
                    "[remove features] cluster has no cache after remove");
        map.zoomIn();
        t.eq(layer.features.length, 0,
                    "[remove features] layer has no features after zoom");
        t.ok(!strategy.features,
                    "[remove features] cluster has no cache after zoom");

        map.destroy();
    }

    function test_deactivate(t) {
        t.plan(2);

        var strategy = new OpenLayers.Strategy.Cluster();
        var layer = new OpenLayers.Layer.Vector("Vector Layer", {
            strategies: [strategy]
        });
        var map = new OpenLayers.Map('map');
        map.addLayer(layer);
        
        t.eq(strategy.active, true, "active after adding to map");
        
        map.removeLayer(layer);
        t.eq(strategy.active, false, "not active after removing from map");
    }

  </script>
</head>
<body>
    <div id="map" style="width: 400px; height: 200px" />
</body>
</html>
